package clusterapi

import (
	"fmt"
	"sync"
)

// ManagerFactory is a factory function for creating cluster managers
type ManagerFactory func(config *Config) (Manager, error)

var (
	// managerFactories is a map of manager types to factory functions
	managerFactories = make(map[string]ManagerFactory)
	// mu is a mutex for protecting the manager factories
	mu sync.RWMutex
)

// RegisterManagerFactory registers a manager factory
func RegisterManagerFactory(name string, factory ManagerFactory) {
	mu.Lock()
	defer mu.Unlock()
	managerFactories[name] = factory
}

// NewManager creates a new cluster manager
func NewManager(managerType string, config *Config) (Manager, error) {
	mu.RLock()
	defer mu.RUnlock()

	// Get factory
	factory, ok := managerFactories[managerType]
	if !ok {
		return nil, fmt.Errorf("unknown cluster manager type: %s", managerType)
	}

	// Create manager
	return factory(config)
}
